package cn.edu.sgu.www.mhxysy.controller.gang;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.gang.GangSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.gang.GangSkillCategory;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.gang.GangSkillCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "帮派技能类型管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/gang_skill_category", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class GangSkillCategoryController {

	private final GangSkillCategoryService gangSkillCategoryService;

	@Autowired
	public GangSkillCategoryController(GangSkillCategoryService gangSkillCategoryService) {
		this.gangSkillCategoryService = gangSkillCategoryService;
	}

	@ApiOperation("上传图片")
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public JsonResult<String> upload(MultipartFile file) throws IOException {
		String url = gangSkillCategoryService.upload(file);

		return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
	}

	@ApiOperation("添加帮派技能类型")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated GangSkillCategoryDTO gangSkillCategoryDTO) {
		gangSkillCategoryService.insert(gangSkillCategoryDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID修改帮派技能类型信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated GangSkillCategoryDTO gangSkillCategoryDTO) {
		gangSkillCategoryService.updateById(gangSkillCategoryDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("通过ID修改帮派技能图片")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "帮派技能类型ID", required = true),
			@ApiImplicitParam(name = "image", value = "图片地址", required = true)
	})
	@RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
	public JsonResult<Void> updateImageById(
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
			@NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
		gangSkillCategoryService.updateImageById(id, image);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("分页查询帮派技能类型列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<GangSkillCategory>> selectByPage(Pager<GangSkillCategory> pager) {
		Page<GangSkillCategory> result = gangSkillCategoryService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}